<!DOCTYPE html>
<html>
<head>
<title>day07</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) */
/* Author: Nicolas Hery - http://nicolashery.com */
/* Version: b13fe65ca28d2e568c6ed5d7f06581183df8f2ff */
/* Source: https://github.com/nicolahery/markdownpad-github */

/* RESET
=============================================================================*/

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
}

/* BODY
=============================================================================*/

body {
  font-family: Helvetica, arial, freesans, clean, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  color: #333;
  background-color: #fff;
  padding: 20px;
  max-width: 960px;
  margin: 0 auto;
}

body>*:first-child {
  margin-top: 0 !important;
}

body>*:last-child {
  margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
  margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
  margin: 20px 0 10px;
  padding: 0;
  font-weight: bold;
  -webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
  font-size: inherit;
}

h1 {
  font-size: 28px;
  color: #000;
}

h2 {
  font-size: 24px;
  border-bottom: 1px solid #ccc;
  color: #000;
}

h3 {
  font-size: 18px;
}

h4 {
  font-size: 16px;
}

h5 {
  font-size: 14px;
}

h6 {
  color: #777;
  font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
  margin-top: 0;
  padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
  margin-top: 0;
  padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
  margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
  color: #4183C4;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
  padding-left: 30px;
}

ul li > :first-child, 
ol li > :first-child, 
ul li ul:first-of-type, 
ol li ol:first-of-type, 
ul li ol:first-of-type, 
ol li ul:first-of-type {
  margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
  margin-bottom: 0;
}

dl {
  padding: 0;
}

dl dt {
  font-size: 14px;
  font-weight: bold;
  font-style: italic;
  padding: 0;
  margin: 15px 0 5px;
}

dl dt:first-child {
  padding: 0;
}

dl dt>:first-child {
  margin-top: 0px;
}

dl dt>:last-child {
  margin-bottom: 0px;
}

dl dd {
  margin: 0 0 15px;
  padding: 0 15px;
}

dl dd>:first-child {
  margin-top: 0px;
}

dl dd>:last-child {
  margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
  font-size: 12px;
  font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
  margin: 0 0px;
  padding: 0px 0px;
  white-space: nowrap;
  border: 1px solid #eaeaea;
  background-color: #f8f8f8;
  border-radius: 3px;
}

pre>code {
  margin: 0;
  padding: 0;
  white-space: pre;
  border: none;
  background: transparent;
}

pre {
  background-color: #f8f8f8;
  border: 1px solid #ccc;
  font-size: 13px;
  line-height: 19px;
  overflow: auto;
  padding: 6px 10px;
  border-radius: 3px;
}

pre code, pre tt {
  background-color: transparent;
  border: none;
}

kbd {
    -moz-border-bottom-colors: none;
    -moz-border-left-colors: none;
    -moz-border-right-colors: none;
    -moz-border-top-colors: none;
    background-color: #DDDDDD;
    background-image: linear-gradient(#F1F1F1, #DDDDDD);
    background-repeat: repeat-x;
    border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
    border-image: none;
    border-radius: 2px 2px 2px 2px;
    border-style: solid;
    border-width: 1px;
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    line-height: 10px;
    padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
  border-left: 4px solid #DDD;
  padding: 0 15px;
  color: #777;
}

blockquote>:first-child {
  margin-top: 0px;
}

blockquote>:last-child {
  margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
  clear: both;
  margin: 15px 0;
  height: 0px;
  overflow: hidden;
  border: none;
  background: transparent;
  border-bottom: 4px solid #ddd;
  padding: 0;
}

/* TABLES
=============================================================================*/

table th {
  font-weight: bold;
}

table th, table td {
  border: 1px solid #ccc;
  padding: 6px 13px;
}

table tr {
  border-top: 1px solid #ccc;
  background-color: #fff;
}

table tr:nth-child(2n) {
  background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
  max-width: 100%
}
</style>
</head>
<body>
<h1>Servlet/JSP</h1>
<h2>回顾</h2>
<p>重定向： 服务器向客户端发送状态码 302 并且发送响应头Location，客户端根据状态码和响应头重新请求 Location 指向的URL地址。</p>
<h2>实现“领导”列表</h2>
<p>更新添加界面，实现“领导”列表，原理为：</p>
<p><img src="mgrs.png" /></p>
<p>步骤:</p>
<ol>
<li>
<p>更新 EmpDao 添加方法查询所有的领导： </p>
<pre><code>/**
 * 获取员工中的全部领导
 */
public List&lt;Emp&gt; findMgrs(){
    String sql = &quot;select empno, ename, mgr, &quot;
            + &quot;hiredate, deptno, salary, comm &quot;
            + &quot;from t_emp where mgr=0 or mgr=1&quot;;
    try(Connection conn=DBUtil.getConnection()){
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        List&lt;Emp&gt; list=new ArrayList&lt;&gt;();
        while(rs.next()) {
            //必须是没有编译错误，并且选定的代码块能够抽取为方法！
            Emp emp = row2Emp(rs);
            list.add(emp);
        }
        return list;
    }catch(Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}


private Emp row2Emp(ResultSet rs) throws SQLException {
    int empno = rs.getInt(&quot;empno&quot;);
    String ename = rs.getString(&quot;ename&quot;);
    int mgr = rs.getInt(&quot;mgr&quot;);
    Date hiredate=rs.getDate(&quot;hiredate&quot;);
    int deptno = rs.getInt(&quot;deptno&quot;);
    double salary = rs.getDouble(&quot;salary&quot;);
    double comm = rs.getDouble(&quot;comm&quot;);
    Emp emp = new Emp(empno, ename, mgr, hiredate, deptno, salary, comm);
    return emp;
}
</code></pre>

</li>
<li>
<p>测试</p>
<pre><code>@Test
public void testFindMgrs() {
    EmpDao dao = new EmpDao();
    List&lt;Emp&gt; list = dao.findMgrs();
    for (Emp emp : list) {
        System.out.println(emp); 
    }
}
</code></pre>

</li>
<li>
<p>添加 AddEmpServlet</p>
<pre><code>public class AddEmpServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * URL /add-emp
     */
    protected void doGet(
            HttpServletRequest request, 
            HttpServletResponse response) 
        throws ServletException, IOException {
        EmpDao dao = new EmpDao();
        List&lt;Emp&gt; mgrs = dao.findMgrs();
        request.setAttribute(&quot;mgrs&quot;, mgrs);
        request.getRequestDispatcher(
                &quot;/WEB-INF/jsp/add-emp.jsp&quot;)
                .forward(request, response);
    }
}
</code></pre>

</li>
<li>
<p>配置</p>
<pre><code>  &lt;servlet&gt;
    &lt;description&gt;&lt;/description&gt;
    &lt;display-name&gt;AddEmpServlet&lt;/display-name&gt;
    &lt;servlet-name&gt;AddEmpServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;day07.AddEmpServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;AddEmpServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/add-emp&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
</code></pre>

</li>
<li>
<p>将 add-emp.jsp 移动到 /WEB-INF/jsp/add-emp.jsp 并且更新：</p>
<pre><code>&lt;div class=&quot;form-group&quot;&gt;
  &lt;label for=&quot;mgr&quot; 
  class=&quot;col-sm-2 control-label&quot;&gt;领导&lt;/label&gt;
  &lt;div class=&quot;col-sm-10&quot;&gt;
    &lt;%
      List&lt;Emp&gt; mgrs=(List&lt;Emp&gt;)request.getAttribute(&quot;mgrs&quot;);
    %&gt;
    &lt;select class=&quot;form-control&quot; id=&quot;mgr&quot; name=&quot;mgr&quot;&gt;
      &lt;%for(Emp mgr:mgrs){%&gt;
      &lt;!-- 客户端选中员工名，发送到服务器是对应的员工号
       value属性是发送到服务器上的值 --&gt;
      &lt;option value=&quot;&lt;%=mgr.getEmpno()%&gt;&quot;&gt;
        &lt;%=mgr.getEname()%&gt;
      &lt;/option&gt;
      &lt;%}%&gt;
    &lt;/select&gt;
  &lt;/div&gt;
&lt;/div&gt;
</code></pre>

</li>
<li>
<p>在 add-emp.jsp 添加导入</p>
<pre><code>import=&quot;java.util.*,entity.*&quot;
</code></pre>

</li>
<li>
<p>重构 list-emp2.jsp</p>
<pre><code>&lt;h3 class=&quot;box-title&quot;&gt;员工信息 &lt;a href=&quot;add-emp&quot; class=&quot;btn btn-success&quot;&gt;添加&lt;/a&gt;&lt;/h3&gt;
</code></pre>

</li>
<li>
<p>测试</p>
</li>
</ol>
<h2>实现修改员工信息功能</h2>
<p>原理：</p>
<p><img src="update.png" /></p>
<p>实现步骤：</p>
<ol>
<li>
<p>EmpDao中添加查找和更新的方法</p>
<pre><code>/**
 * 根据ID获取一个员工信息
 */
public Emp findByEmpno(int empno) {
    String sql = &quot;select empno, ename, mgr, &quot; + 
            &quot;hiredate, deptno, salary, comm &quot; + 
            &quot;from t_emp where empno=?&quot;;
    try(Connection conn = DBUtil.getConnection()){
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setInt(1, empno);
        ResultSet rs = ps.executeQuery();
        Emp emp=null;
        while(rs.next()) {
            emp = row2Emp(rs); //重用 row2Emp 方法
        }
        return emp; //找到结果返回emp对象，
        //找不到返回null表示&quot;空&quot;结果。
    }catch(Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

/**
 * 根据ID更新员工信息
 */
public int updateEmp(Emp emp) {
    String sql = &quot;update t_emp set ename=?, &quot;
            + &quot;mgr=?, hiredate=?, deptno=?,&quot;
            + &quot;salary=?, comm=? &quot;
            + &quot;where empno=?&quot;;
    try(Connection conn = DBUtil.getConnection()){
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1, emp.getEname());
        ps.setInt(2, emp.getMgr());
        ps.setDate(3, emp.getHiredate());
        ps.setInt(4, emp.getDeptno());
        ps.setDouble(5, emp.getSalary());
        ps.setDouble(6, emp.getComm());
        ps.setInt(7, emp.getEmpno());
        int n = ps.executeUpdate();
        return n;
    }catch(Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
</code></pre>

</li>
<li>
<p>测试：</p>
<pre><code>@Test
public void testFindUpdate() {
    EmpDao dao = new EmpDao();
    Emp emp = dao.findByEmpno(2);
    System.out.println(emp);
    emp.setEname(&quot;杰瑞&quot;);
    int n = dao.updateEmp(emp);
    System.out.println(n); 
}
</code></pre>

</li>
<li>
<p>添加 EditEmpServlet</p>
<pre><code>public class EditEmpServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * 处理 get 请求 URL /edit-emp
     */
    protected void doGet(
        HttpServletRequest request, 
        HttpServletResponse response) 
            throws ServletException, IOException {
        String no = request.getParameter(&quot;empno&quot;);
        int empno = Integer.parseInt(no);
        EmpDao dao = new EmpDao();
        Emp emp = dao.findByEmpno(empno);

        List&lt;Emp&gt; mgrs = dao.findMgrs();

        request.setAttribute(&quot;mgrs&quot;, mgrs);
        request.setAttribute(&quot;emp&quot;, emp);
        request.getRequestDispatcher(
                &quot;/WEB-INF/jsp/edit-emp.jsp&quot;)
                .forward(request, response); 
    }

}
</code></pre>

</li>
<li>
<p>配置</p>
<pre><code>&lt;servlet&gt;
    &lt;description&gt;&lt;/description&gt;
    &lt;display-name&gt;EditEmpServlet&lt;/display-name&gt;
    &lt;servlet-name&gt;EditEmpServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;day07.EditEmpServlet&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
    &lt;servlet-name&gt;EditEmpServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/edit-emp&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</code></pre>

</li>
<li>
<p>编写 edit-emp.jsp</p>
<pre><code>  &lt;%
    //获取EditEmpServlet共享的 emp 对象
    Emp emp = (Emp) request.getAttribute(&quot;emp&quot;); 
  %&gt;        
  &lt;!-- Horizontal Form --&gt;
  &lt;div class=&quot;box box-info&quot;&gt;
    &lt;div class=&quot;box-header with-border&quot;&gt;
      &lt;h3 class=&quot;box-title&quot;&gt;添加新员工信息&lt;/h3&gt;
    &lt;/div&gt;
    &lt;!-- /.box-header --&gt;
    &lt;!-- form start --&gt;
    &lt;form class=&quot;form-horizontal&quot; 
      method=&quot;post&quot; action=&quot;update-emp&quot;&gt; 
      &lt;div class=&quot;box-body&quot;&gt;
        &lt;div class=&quot;form-group&quot;&gt;
          &lt;label for=&quot;empno&quot; 
            class=&quot;col-sm-2 control-label&quot;&gt;员工号&lt;/label&gt;
          &lt;div class=&quot;col-sm-10&quot;&gt;
            &lt;input type=&quot;text&quot; class=&quot;form-control&quot; 
              id=&quot;empno&quot; name=&quot;empno&quot; placeholder=&quot;员工号&quot;
              value=&quot;&lt;%=emp.getEmpno()%&gt;&quot;
              readonly=&quot;readonly&quot; &gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;form-group&quot;&gt;
          &lt;label for=&quot;ename&quot; 
            class=&quot;col-sm-2 control-label&quot;&gt;姓名&lt;/label&gt;
          &lt;div class=&quot;col-sm-10&quot;&gt;
            &lt;input type=&quot;text&quot; class=&quot;form-control&quot; 
              id=&quot;ename&quot; name=&quot;ename&quot; placeholder=&quot;姓名&quot;
              value=&quot;&lt;%=emp.getEname()%&gt;&quot;&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;form-group&quot;&gt;
          &lt;label for=&quot;mgr&quot; 
          class=&quot;col-sm-2 control-label&quot;&gt;领导&lt;/label&gt;
          &lt;div class=&quot;col-sm-10&quot;&gt;
            &lt;%
              List&lt;Emp&gt; mgrs=(List&lt;Emp&gt;)request.getAttribute(&quot;mgrs&quot;);
            %&gt;
            &lt;select class=&quot;form-control&quot; id=&quot;mgr&quot; name=&quot;mgr&quot;&gt;
              &lt;%for(Emp mgr:mgrs){%&gt;
              &lt;!-- 客户端选中员工名，发送到服务器是对应的员工号
               value属性是发送到服务器上的值 --&gt;
              &lt;%  if(mgr.getEmpno()==emp.getMgr()){%&gt;
                    &lt;option value=&quot;&lt;%=mgr.getEmpno()%&gt;&quot; 
                      selected=&quot;selected&quot;&gt;
                      &lt;%=mgr.getEname()%&gt;
                    &lt;/option&gt;
              &lt;%  } else { %&gt;
                    &lt;option value=&quot;&lt;%=mgr.getEmpno()%&gt;&quot; &gt;
                      &lt;%=mgr.getEname()%&gt;
                    &lt;/option&gt;                     
              &lt;%  }%&gt;
              &lt;%}%&gt;
            &lt;/select&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;form-group&quot;&gt;
          &lt;label for=&quot;hiredate&quot; 
          class=&quot;col-sm-2 control-label&quot;&gt;入职日期&lt;/label&gt;
          &lt;div class=&quot;col-sm-10&quot;&gt;
            &lt;input type=&quot;date&quot; class=&quot;form-control&quot; 
            id=&quot;hiredate&quot; name=&quot;hiredate&quot;  
            placeholder=&quot;入职日期&quot; 
            value=&quot;&lt;%=emp.getHiredate()%&gt;&quot;&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;form-group&quot;&gt;
          &lt;label for=&quot;deptno&quot; 
          class=&quot;col-sm-2 control-label&quot;&gt;部门编号&lt;/label&gt;
          &lt;div class=&quot;col-sm-10&quot;&gt;
            &lt;input type=&quot;text&quot; class=&quot;form-control&quot; 
            id=&quot;deptno&quot; name=&quot;deptno&quot;  
            placeholder=&quot;部门编号&quot;
            value=&quot;&lt;%=emp.getDeptno()%&gt;&quot;&gt;
          &lt;/div&gt;
        &lt;/div&gt;  
        &lt;div class=&quot;form-group&quot;&gt;
          &lt;label for=&quot;salary&quot; 
          class=&quot;col-sm-2 control-label&quot;&gt;薪资&lt;/label&gt;
          &lt;div class=&quot;col-sm-10&quot;&gt;
            &lt;input type=&quot;number&quot; class=&quot;form-control&quot; 
            id=&quot;salary&quot; name=&quot;salary&quot;  
            placeholder=&quot;薪资&quot;
            value=&quot;&lt;%=emp.getSalary()%&gt;&quot;&gt;
          &lt;/div&gt;
        &lt;/div&gt;  
        &lt;div class=&quot;form-group&quot;&gt;
          &lt;label for=&quot;comm&quot; 
          class=&quot;col-sm-2 control-label&quot;&gt;提成&lt;/label&gt;
          &lt;div class=&quot;col-sm-10&quot;&gt;
            &lt;input type=&quot;number&quot; class=&quot;form-control&quot; 
            id=&quot;comm&quot; name=&quot;comm&quot;  
            placeholder=&quot;提成&quot;
            value=&quot;&lt;%=emp.getComm()%&gt;&quot;&gt;
          &lt;/div&gt;
        &lt;/div&gt;                                
      &lt;/div&gt;
      &lt;!-- /.box-body --&gt;
      &lt;div class=&quot;box-footer&quot;&gt;
        &lt;button type=&quot;submit&quot; class=&quot;btn btn-info pull-right&quot;&gt;保存&lt;/button&gt;
      &lt;/div&gt;
      &lt;!-- /.box-footer --&gt;
    &lt;/form&gt;
  &lt;/div&gt;
  &lt;!-- /.box --&gt;
</code></pre>

</li>
<li>
<p>测试，显示编辑界面</p>
<pre><code>http://localhost:8080/Servlet07/edit-emp?empno=3
</code></pre>

</li>
<li>
<p>编写UpdateEmpServlet 处理表单Post请求</p>
<pre><code>public class UpdateEmpServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * 处理 post 请求， URL: /update-emp 
     */
    protected void doPost(
            HttpServletRequest request, 
            HttpServletResponse response) 
        throws ServletException, IOException {
        request.setCharacterEncoding(&quot;UTF-8&quot;);
        String no = request.getParameter(&quot;empno&quot;);
        String ename=request.getParameter(&quot;ename&quot;);
        String mgrno=request.getParameter(&quot;mgr&quot;);
        String hire=request.getParameter(&quot;hiredate&quot;);
        String dept=request.getParameter(&quot;deptno&quot;);
        String sly =request.getParameter(&quot;salary&quot;);
        String com =request.getParameter(&quot;comm&quot;);
        int empno = Integer.parseInt(no);
        int mgr = Integer.parseInt(mgrno);
        //java.sql.Date 提供了将字符串转换为日期的方法
        Date hiredate = Date.valueOf(hire);
        int deptno = Integer.parseInt(dept);
        double salary = Double.parseDouble(sly);
        double comm = Double.parseDouble(com);

        Emp emp = new Emp(empno, ename, mgr, 
            hiredate, deptno, salary, comm);
        EmpDao dao = new EmpDao();
        //更新数据
        int n = dao.updateEmp(emp);
        if(n==1) {
            String path=request.getContextPath()+
                    &quot;/list-emp&quot;;
            response.sendRedirect(path); 
        } else {
            request.setAttribute(&quot;message&quot;, 
                    &quot;更新失败！&quot;);
            request.getRequestDispatcher(
                    &quot;/WEB-INF/jsp/message.jsp&quot;)
                    .forward(request, response);
        }
    }
}
</code></pre>

</li>
<li>
<p>更新 list-emp2.jsp 添加编辑员工信息按钮</p>
<pre><code>  &lt;td&gt;&lt;a onclick=&quot;return confirm('真的删除?吗?')&quot; 
    href=&quot;delete?empno=&lt;%=emp.getEmpno()%&gt;&quot; 
    class=&quot;btn btn-danger btn-xs&quot;
    &gt;&lt;i class=&quot;fa fa-fw fa-remove&quot;&gt;&lt;/i&gt;&lt;/a&gt;
    &lt;a href=&quot;edit-emp?empno=&lt;%=emp.getEmpno()%&gt;&quot;
      class=&quot;btn btn-success btn-xs&quot;&gt;&lt;i 
      class=&quot;fa fa-fw fa-edit&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/td&gt;
</code></pre>

</li>
<li>
<p>测试</p>
</li>
</ol>
<h2>网页的包含</h2>
<p>一个网页包含了另外一个网页组件， 最后合并为一个页面，称为包含</p>
<p>JSP提供两个包含方式</p>
<ol>
<li>
<p>静态包含： 翻译期间会将两个JSP合并为一个Servlet，运行期间执行一个Servlet</p>
<pre><code>&lt;%@ include file=&quot;从页面文件地址&quot;%&gt;
</code></pre>

</li>
<li>
<p>动态包含： 翻译期间生成两个Servlet，运行期间一个Serlvet动态调用了另外一个Servlet</p>
<pre><code>&lt;jsp:include page=&quot;header.jsp&quot;&gt;&lt;/jsp:include&gt;   
</code></pre>

</li>
</ol>
<blockquote>
<p>两个包含方式，结果差别不大，一般采用动态包含编程更加方便。</p>
</blockquote>
<p>案例：</p>
<ol>
<li>
<p>被包含页面 header.jsp</p>
<pre><code>&lt;%@ page 
  language=&quot;java&quot; 
  contentType=&quot;text/html; charset=UTF-8&quot;
  pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;div&gt;
  头部
&lt;/div&gt;
</code></pre>

</li>
<li>
<p>静态包含：</p>
<pre><code>&lt;%@ page 
  language=&quot;java&quot; 
  contentType=&quot;text/html; charset=UTF-8&quot;
  pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;!-- 静态包含 header.jsp, 两个网页在翻译期间合并为同一个 --&gt;
  &lt;%@ include file=&quot;header.jsp&quot; %&gt;
  &lt;div&gt;
    内容
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

</li>
<li>
<p>动态包含：</p>
<pre><code>&lt;%@ page 
  language=&quot;java&quot; 
  contentType=&quot;text/html; charset=UTF-8&quot;
  pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;!-- 动态 header.jsp, 两个网页分别编译为Servlet --&gt;
  &lt;jsp:include page=&quot;header.jsp&quot;&gt;&lt;/jsp:include&gt; 
  &lt;div&gt;
    内容
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

</li>
</ol>
<h2>将员工管理拆分为包含方式：</h2>
<ol>
<li>
创建页面组件 /WEB-INF/include
<ol>
<li>header.jsp</li>
<li>footer.jsp</li>
<li>left-side.jsp</li>
<li>sidebar.jsp</li>
</ol>
</li>
<li>
<p>重构页面 list-emp2.jsp</p>
<pre><code>&lt;%@ page 
    language=&quot;java&quot; 
    contentType=&quot;text/html; charset=UTF-8&quot;
    pageEncoding=&quot;UTF-8&quot;
    import=&quot;java.util.*,entity.*&quot;
%&gt;
&lt;!DOCTYPE html&gt;
&lt;!--
This is a starter template page. Use this page to start your new project from
scratch. This page gets rid of all links and provides the needed markup only.
--&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;meta charset=&quot;utf-8&quot;&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  &lt;title&gt;AdminLTE 2 | Starter&lt;/title&gt;
  &lt;!-- Tell the browser to be responsive to screen width --&gt;
  &lt;meta content=&quot;width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no&quot; name=&quot;viewport&quot;&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;bower_components/bootstrap/dist/css/bootstrap.min.css&quot;&gt;
  &lt;!-- Font Awesome --&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;bower_components/font-awesome/css/font-awesome.min.css&quot;&gt;
  &lt;!-- Ionicons --&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;bower_components/Ionicons/css/ionicons.min.css&quot;&gt;
  &lt;!-- Theme style --&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;dist/css/AdminLTE.min.css&quot;&gt;
  &lt;!-- AdminLTE Skins. We have chosen the skin-blue for this starter
        page. However, you can choose any other skin. Make sure you
        apply the skin class to the body tag so the changes take effect. --&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;dist/css/skins/skin-blue.min.css&quot;&gt;

  &lt;!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --&gt;
  &lt;!-- WARNING: Respond.js doesn't work if you view the page via file:// --&gt;
  &lt;!--[if lt IE 9]&gt;
  &lt;script src=&quot;https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://oss.maxcdn.com/respond/1.4.2/respond.min.js&quot;&gt;&lt;/script&gt;
  &lt;![endif]--&gt;

&lt;/head&gt;
&lt;!--
BODY TAG OPTIONS:
=================
Apply one or more of the following classes to get the
desired effect
|---------------------------------------------------------|
| SKINS         | skin-blue                               |
|               | skin-black                              |
|               | skin-purple                             |
|               | skin-yellow                             |
|               | skin-red                                |
|               | skin-green                              |
|---------------------------------------------------------|
|LAYOUT OPTIONS | fixed                                   |
|               | layout-boxed                            |
|               | layout-top-nav                          |
|               | sidebar-collapse                        |
|               | sidebar-mini                            |
|---------------------------------------------------------|
--&gt;
&lt;body class=&quot;hold-transition skin-blue sidebar-mini&quot;&gt;
&lt;div class=&quot;wrapper&quot;&gt;

  &lt;!-- Main Header 包含子页面  --&gt;
  &lt;jsp:include page=&quot;/WEB-INF/include/header.jsp&quot;&gt;&lt;/jsp:include&gt;

  &lt;!-- Left side 包含子页面 --&gt;
  &lt;jsp:include page=&quot;/WEB-INF/include/left-side.jsp&quot;&gt;&lt;/jsp:include&gt;

  &lt;!-- Content Wrapper. Contains page content --&gt;
  &lt;div class=&quot;content-wrapper&quot;&gt;
    &lt;!-- Content Header (Page header) --&gt;
    &lt;section class=&quot;content-header&quot;&gt;
      &lt;h1&gt;
        Page Header
        &lt;small&gt;Optional description&lt;/small&gt;
      &lt;/h1&gt;
      &lt;ol class=&quot;breadcrumb&quot;&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&lt;i class=&quot;fa fa-dashboard&quot;&gt;&lt;/i&gt; Level&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;active&quot;&gt;Here&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/section&gt;

    &lt;!-- Main content --&gt;
    &lt;section class=&quot;content container-fluid&quot;&gt;

      &lt;!--------------------------
        | Your Page Content Here |
        --------------------------&gt;
          &lt;div class=&quot;box&quot;&gt;
            &lt;div class=&quot;box-header with-border&quot;&gt;
              &lt;h3 class=&quot;box-title&quot;&gt;员工信息  
                 &lt;a href=&quot;add-emp&quot; class=&quot;btn btn-success&quot;&gt;&lt;i class=&quot;fa fa-user-plus&quot;&gt;&lt;/i&gt; 添加&lt;/a&gt;&lt;/h3&gt;
            &lt;/div&gt;
            &lt;!-- /.box-header --&gt;
            &lt;div class=&quot;box-body&quot;&gt;
              &lt;table class=&quot;table table-bordered&quot;&gt;
                &lt;tr&gt;
                  &lt;th style=&quot;width: 10px&quot;&gt;#&lt;/th&gt;
                  &lt;th&gt;姓名&lt;/th&gt;
                  &lt;th&gt;老板&lt;/th&gt;
                  &lt;th&gt;入职日期&lt;/th&gt;
                  &lt;th&gt;部门编号&lt;/th&gt;
                  &lt;th&gt;薪资&lt;/th&gt;
                  &lt;th&gt;提成&lt;/th&gt;
                  &lt;th&gt;&lt;/th&gt;
                &lt;/tr&gt;
&lt;% 
  List&lt;Emp&gt; list =
      (List&lt;Emp&gt;)request.getAttribute(&quot;list&quot;);
%&gt;              
                &lt;%for(Emp emp:list){%&gt;  
                &lt;tr&gt;
                  &lt;td&gt;&lt;%=emp.getEmpno()%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%=emp.getEname()%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%=emp.getMgr()%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%=emp.getHiredate()%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%=emp.getDeptno()%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%=emp.getSalary()%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%=emp.getComm()%&gt;&lt;/td&gt;
                  &lt;!-- onclick中出现&quot;return false&quot; 则会阻止事件
                  -点击事件的响应 --&gt;
                  &lt;td&gt;&lt;a onclick=&quot;return confirm('真的删除?吗?')&quot; 
                    href=&quot;delete?empno=&lt;%=emp.getEmpno()%&gt;&quot; 
                    class=&quot;btn btn-danger btn-xs&quot;
                    &gt;&lt;i class=&quot;fa fa-fw fa-remove&quot;&gt;&lt;/i&gt;&lt;/a&gt;
                    &lt;a href=&quot;edit-emp?empno=&lt;%=emp.getEmpno()%&gt;&quot;
                      class=&quot;btn btn-success btn-xs&quot;&gt;&lt;i 
                      class=&quot;fa fa-fw fa-edit&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;%}%&gt;
              &lt;/table&gt;
            &lt;/div&gt;
            &lt;!-- /.box-body --&gt;
            &lt;div class=&quot;box-footer clearfix&quot;&gt;
              &lt;ul class=&quot;pagination pagination-sm no-margin pull-right&quot;&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&amp;laquo;&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;1&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;2&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;3&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&amp;raquo;&lt;/a&gt;&lt;/li&gt;
              &lt;/ul&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- /.box --&gt;



    &lt;/section&gt;
    &lt;!-- /.content --&gt;
  &lt;/div&gt;
  &lt;!-- /.content-wrapper --&gt;

  &lt;!-- Main Footer 包含 --&gt;
  &lt;jsp:include page=&quot;/WEB-INF/include/footer.jsp&quot;&gt;&lt;/jsp:include&gt;

  &lt;!-- Control Sidebar 包含 --&gt;
  &lt;jsp:include page=&quot;/WEB-INF/include/sidebar.jsp&quot;&gt;&lt;/jsp:include&gt;

&lt;/div&gt;
&lt;!-- ./wrapper --&gt;

&lt;!-- REQUIRED JS SCRIPTS --&gt;

&lt;!-- jQuery 3 --&gt;
&lt;script src=&quot;bower_components/jquery/dist/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;!-- Bootstrap 3.3.7 --&gt;
&lt;script src=&quot;bower_components/bootstrap/dist/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;!-- AdminLTE App --&gt;
&lt;script src=&quot;dist/js/adminlte.min.js&quot;&gt;&lt;/script&gt;

&lt;!-- Optionally, you can add Slimscroll and FastClick plugins.
     Both of these plugins are recommended to enhance the
     user experience. --&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

</li>
<li>
<p>其他页面略</p>
</li>
</ol>
<h2>作业</h2>
<ol>
<li>重构页面，实现页面组件的包含</li>
<li>重构上次作业案例，实现页面组件的包含</li>
<li>重写实现完整的 部门表增删改查</li>
</ol>

</body>
</html>
<!-- This document was created with MarkdownPad, the Markdown editor for Windows (http://markdownpad.com) -->
